package com.gzs.zhdj.service.impl;

import com.gzs.zhdj.constant.MessageConstant;
import com.gzs.zhdj.constant.RoleConstant;
import com.gzs.zhdj.context.BaseContext;
import com.gzs.zhdj.domain.dto.PartyMemberDto;
import com.gzs.zhdj.domain.dto.PasswordEditDto;
import com.gzs.zhdj.domain.entity.PartyMember;
import com.gzs.zhdj.exception.AccountErrorException;
import com.gzs.zhdj.exception.FileErrorException;
import com.gzs.zhdj.exception.PasswordEditFailedException;
import com.gzs.zhdj.exception.PasswordErrorException;
import com.gzs.zhdj.mapper.PartyMemberMapper;
import com.gzs.zhdj.service.PartyMemberService;
import com.gzs.zhdj.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
@Slf4j
public class PartyMemberServiceImpl implements PartyMemberService {
    @Autowired
    private PartyMemberMapper partyMemberMapper;
    @Autowired
    private AliOssUtil aliOssUtil;

    public PartyMember partyMemberLogin(PartyMemberDto partyMemberDto) {
        int id = partyMemberDto.getId();

        String password = partyMemberDto.getPassword();
        PartyMember partyMember = partyMemberMapper.getById(id);
        if(!(partyMember.getRole()).equals(RoleConstant.PARTYMEMBER))
        {
            throw new AccountErrorException(MessageConstant.ADMIN_ROLR_ERROR);
        }
        if (partyMember == null) {
            //账号不存在
            throw new AccountErrorException(MessageConstant.ACCOUNT_ERROR);
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(partyMember.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return partyMember;
    }
    @Override
    public PartyMember getSelfInfo() {
        int id = BaseContext.getCurrentId();
        PartyMember member = partyMemberMapper.getBySelfId(id);
        return member;
    }
    //修改密码
    public void updatePassword(PasswordEditDto passwordEditDto) {
        int id = BaseContext.getCurrentId();
        PartyMember partyMember = partyMemberMapper.getBySelfId(id);
        if (passwordEditDto.getOldPassword().equals(passwordEditDto.getNewPassword2())) {
            throw new PasswordEditFailedException(MessageConstant.O_N_PASSWORD_ERROR);
        }
        if (!DigestUtils.md5DigestAsHex(passwordEditDto.getOldPassword().getBytes()).equals(partyMember.getPassword())) {
            throw new PasswordEditFailedException(MessageConstant.OLD_PASSWORD_ERROR);
        }
        if (!passwordEditDto.getNewPassword1().equals(passwordEditDto.getNewPassword2())) {
            throw new PasswordEditFailedException(MessageConstant.NEW_PASSWORD_DIFFERENT);
        }
        String encryptedPassword = DigestUtils.md5DigestAsHex(passwordEditDto.getNewPassword2().getBytes());
        passwordEditDto.setId(id);
        passwordEditDto.setNewPassword2(encryptedPassword);
        partyMemberMapper.updatePassword(passwordEditDto);
    }

    @Override
    public void upload(MultipartFile file) {
        try {
            int id = BaseContext.getCurrentId();
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //检查文件格式是否为图片
            String[] allowedExtensions = {".jpg", ".jpeg", ".png", ".gif", ".bmp"};
            boolean isValidFormat = false;
            for (String ext : allowedExtensions) {
                if (extension.equals(ext)) {
                    isValidFormat = true;
                    break;
                }
            }
            if (!isValidFormat) {
             throw new FileErrorException("文件格式错误，只允许上传图片格式文件");
            }
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;

            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
                partyMemberMapper.upload(id, filePath);
        } catch (IOException e) {
//            throw new F
        }

    }

    @Override
    public void updateSelfInof(PartyMember partyMember) {
        int id = BaseContext.getCurrentId();
        partyMember.setId(id);
        partyMemberMapper.updateSelfInfo(partyMember);
    }
}
